# _*_ coding: utf-8 _*_
"""
Time:     2022/3/10 21:39
Author:   suhong
Version:  V 0.1
File:     test.py
"""
from selenium.webdriver.common.by import By


def __convert_data__(selector):
    '''
    将selector数据(id,account)转换为(By.ID,account)格式
    :param selector:
    :return:
    '''
    try:
        selector_by = selector.split(",")[0].strip()
        selector_value = selector.split(",")[1].strip()
    except IndexError:
        selector_by = selector.split("=")[0].strip()
        selector_value = selector.split("=")[1].strip()
    if selector_by == "id":
        locator = (By.ID, selector_value)
    elif selector_by == "xpath":
        locator = (By.XPATH, selector_value)
    elif selector_by == "name":
        locator = (By.NAME, selector_value)
    elif selector_by == "class":
        locator = (By.CLASS_NAME, selector_value)
    elif selector_by == "name":
        locator = (By.NAME, selector_value)
    else:
        raise NameError("输入的参数不对,或者是我没加")
    return locator


def __convert_selector_to_locator(selector):
    """
    将selector数据(id,account)转换为(By.ID,account)格式
    :param selector: 元素定位方式和对应的值 如id,account
    :return:
    """
    try:
        value_list = selector.split("~")
        selector_by = value_list[0].strip()  # 将selector的值切割成对应的方式和值
        selector_value = value_list[1].strip()
    except IndexError:
        raise ValueError("参数错误")

    if selector_by == 'i' or selector_by == 'id':
        locator = (By.ID, selector_value)
    elif selector_by == 'x' or selector_by == 'xpath':
        locator = (By.XPATH, selector_value)

    elif selector_by == 'css' or selector_by == 'css_selector':
        locator = (By.CSS_SELECTOR, selector_value)

    elif selector_by == 'n' or selector_by == 'name':
        locator = (By.NAME, selector_value)

    elif selector_by == 'c' or selector_by == 'class':
        locator = (By.CLASS_NAME, selector_value)

    elif selector_by == 'l' or selector_by == 'linkText':
        locator = (By.LINK_TEXT, selector_value)

    elif selector_by == 'p' or selector_by == 'partial_link_text':
        locator = (By.PARTIAL_LINK_TEXT, selector_value)

    elif selector_by == 't' or selector_by == 'tag_name':
        locator = (By.TAG_NAME, selector_value)

    else:
        raise ValueError("参数不正确")
    return locator


res = __convert_data__("id,123")
print(res)
a = __convert_selector_to_locator("id~123")
print(a)
